package com.dev.web.controller.common;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dev.bbs.domain.BbsUser;
import com.dev.bbs.service.IBbsUserService;
import com.dev.common.annotation.Log;
import com.dev.common.core.controller.BaseController;
import com.dev.common.core.domain.AjaxResult;
import com.dev.common.enums.BusinessType;
import com.dev.framework.shiro.service.BbsPasswordService;
import com.dev.framework.util.ShiroUtils;

/**
 * @ClassName BbsRegisterController
 * @PackageName com.dev.web.controller
 * @Description 注册
 * @Author daiz
 * @Date 2019/9/19 11:05
 * @Version 1.0
 */
@Controller
public class BbsRegisterController extends BaseController
{
    private String             prefix = "common";

    @Autowired
    private IBbsUserService    bbsUserService;

    @Autowired
    private BbsPasswordService passwordService;

    /**
     * 注册页
     *
     * @return
     */
    @GetMapping("/register")
    public String goLogin()
    {
        return prefix + "/reg";
    }

    /**
     * 社区用户注册
     *
     * @param bbsUser
     * @return
     */
    @Log(title = "社区用户注册", businessType = BusinessType.INSERT)
    @PostMapping("/register")
    @ResponseBody
    public AjaxResult doRegister(BbsUser bbsUser, HttpServletRequest request)
    {
        if (bbsUserService.checkUsernameExist(bbsUser.getUsername()))
        {
            return error("用户名已存在");
        }
        if (bbsUserService.checkNicknameExist(bbsUser.getNickname()))
        {
            return error("昵称已存在");
        }
        bbsUser.setSalt(ShiroUtils.randomSalt());
        bbsUser.setPassword(
                passwordService.encryptPassword(bbsUser.getUsername(), bbsUser.getPassword(), bbsUser.getSalt()));
        return toAjax(bbsUserService.insertBbsUser(bbsUser));
    }
}
